package com.p2p.front.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import net.coobird.thumbnailator.Thumbnails;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ImageZoomUtil {
	private static final Logger	log							= LoggerFactory.getLogger(ImageZoomUtil.class);
	
	public static final String	IMAGE_FILE_NAME_REG_L		= "^[a-zA-Z0-9]+_[1-9]L\\.png$";

	public static final String	S_FILE_NAME_SUFFIX_REG_L	= "L\\.png$";

	public static final String	S_FILE_NAME_SUFFIX_M		= "M.png";

	public static final String	S_FILE_NAME_SUFFIX_S		= "S.png";

	public static final int		IMAGE_WIDTH_M				= 622;

	public static final int		IMAGE_HEIGHT_M				= 410;

	public static final int		IMAGE_WIDTH_S				= 105;

	public static final int		IMAGE_HEIGHT_S				= 69;

	/**
	 * 解压缩
	 * 
	 * @param zipfile
	 * @param destDir
	 * @throws IOException
	 */
	public static void unzipResource(File zipfile,String destDir) throws IOException{
		log.debug("the param zipfile:{}", zipfile.toString());
		log.debug("the param destDir:{}", destDir);
		byte b[] = new byte[1024];
		int length;
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipfile));
		ZipEntry entry;
		while ((entry = zipInputStream.getNextEntry()) != null){
			String entryName = entry.getName();
			File destFile = new File(destDir + entryName);
			if (entry.isDirectory()){
				if (!destFile.exists()){
					destFile.mkdirs();
				}
			}else{
				if (!destFile.getParentFile().exists()){
					destFile.getParentFile().mkdirs();
				}
				OutputStream os = new FileOutputStream(destFile);
				while ((length = zipInputStream.read(b)) != -1){
					os.write(b, 0, length);
				}
				os.close();
			}
			zipInputStream.closeEntry();
		}
		log.debug("文件解压成功 ");
	}
	
	/**
	 * 缩放目录内的图片
	 * 
	 * @param folder
	 * @throws IOException
	 */
	public static void zoomFolder(File folder) throws IOException{
		if (!folder.isDirectory()){
			log.error("error:" + folder.getName() + " is not a directory.");
			return;
		}
		log.debug("....start to zoom images from folder....");
		log.debug("folder name: " + folder.getAbsolutePath());
		File[] files = folder.listFiles();
		log.debug("file count: " + files.length);
		for (File f : files){
			if (f.isDirectory()){
				zoomFolder(f);
			}else{
				String fileName = f.getName();
				if (Pattern.matches(IMAGE_FILE_NAME_REG_L, fileName)){
					zoomImgWithSize(f);
				}
			}
		}
		log.debug("....end to zoom images from folder....");
	}

	// 缩放图片到指定尺寸
	public static void zoomImgWithSize(File imgFile) throws IOException{
		if (!Pattern.matches(IMAGE_FILE_NAME_REG_L, imgFile.getName())){
			log.debug("error: " + imgFile.getName() + " is not a large sku image.");
			return;
		}
		log.debug("----start to zoom image----------");
		log.debug("source large image name:" + imgFile.getAbsolutePath());
		// 生成中图
		log.debug("----start to zoom medium image----------");
		log.debug("destinct medium image name:" + imgFile.getAbsolutePath().replaceAll(S_FILE_NAME_SUFFIX_REG_L, S_FILE_NAME_SUFFIX_M));
		zoomImgwithSize(
				imgFile.getAbsolutePath(),
				imgFile.getAbsolutePath().replaceAll(S_FILE_NAME_SUFFIX_REG_L, S_FILE_NAME_SUFFIX_M),
				IMAGE_WIDTH_M,
				IMAGE_HEIGHT_M);
		log.debug("----end to zoom medium image----------");
		// 生成小图
		log.debug("----start to zoom small image----------");
		log.debug("destinct small image name:" + imgFile.getName().replaceAll(S_FILE_NAME_SUFFIX_REG_L, S_FILE_NAME_SUFFIX_S));
		zoomImgwithSize(
				imgFile.getAbsolutePath(),
				imgFile.getAbsolutePath().replaceAll(S_FILE_NAME_SUFFIX_REG_L, S_FILE_NAME_SUFFIX_S),
				IMAGE_WIDTH_S,
				IMAGE_HEIGHT_S);
		log.debug("----end to zoom small image----------");
		log.debug("----end to zoom image----------");
	}
	
	public static void zoomImgwithSize(String srcImgFileName,String destImgFileName,int width,int height) throws IOException{
		Thumbnails.of(srcImgFileName).size(width, height).toFile(destImgFileName);
	}

}
